package TLF 
{
	import fl.controls.Button;
	import flash.display.Sprite;
	import flash.display.Stage;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.text.engine.FontWeight;
	import flash.utils.setTimeout;
	import flashx.textLayout.container.ContainerController;
	import flashx.textLayout.edit.EditManager;
	import flashx.textLayout.edit.SelectionManager;
	import flashx.textLayout.elements.InlineGraphicElement;
	import flashx.textLayout.elements.LinkElement;
	import flashx.textLayout.elements.ParagraphElement;
	import flashx.textLayout.elements.SpanElement;
	import flashx.textLayout.elements.TextFlow;
	import flashx.textLayout.formats.LineBreak;
	
	public class FaceRender extends Sprite
	{
		
		private var userTextFlow:TextFlow;

		
		private var faceButton:Button;
		private var eventButton:Button;
		private var editButton:Button;
		private var resultButton:Button;
		private var _stage:Stage;
		private  var _p:P= new P();
		public function FaceRender() 
		{
			_stage = this.stage;
			
			userTextFlow = new TextFlow();

			
			var editMode:EditManager = new EditManager();			
			userTextFlow.interactionManager = editMode;

			//flashx.textLayout.formats LineBreak.EXPLICIT;
			//userTextFlow.breakOpportunity =  BreakOpportunity.NONE; 
			//flash.text.engine.BreakOpportunity
			///breakOpportunity
			var container:Sprite=new Sprite();
				this.addChild(container);
			var cc:ContainerController = new ContainerController(container, 400,55);
             
			// userTextFlow.addChild(userTextFlowPE);			
             userTextFlow.flowComposer.addController(cc)
			 
             userTextFlow.lineBreak = LineBreak.EXPLICIT;　//一行
			 userTextFlow.flowComposer.updateAllControllers();
			
			// userTextFlow.interactionManager.setFocus();
			// trace(userTextFlow.interactionManager.focused);
			 this.border = true;
		
			 
			 
			   for (var j:int = 0; j <  userTextFlow.numChildren; j++) 
			{				
				 trace(userTextFlow.getChildAt(j))
					//trace( InlineGraphicElement(p.getChildAt(j)).source);
			}
				
				faceButton = new Button();
				faceButton.label = "add face"
			 	faceButton.addEventListener(MouseEvent.CLICK, function():void {
			//	addEventText("开启雷峰塔", "20:90")
			//	addProps("卓越翅膀","909909")
				addFace("tttt.swf",50,50)
				} );
				faceButton.x = 500
				addChild(faceButton);
				
				
				eventButton = new Button();
				eventButton.label = "add event"
				eventButton.x = 500
				eventButton.y = 30;
				eventButton.addEventListener(MouseEvent.CLICK, function():void {
			        addEventText("开启雷峰塔", "20:90", function():void {
							showMenu()
						//trace(_stage.mouseX, _stage.mouseY ,"<<<<<<<<<<<")
						})
				} );
				addChild(eventButton)

				
				editButton = new Button();
				editButton.label = "切换状态"
				editButton.x = 500
				editButton.y = 60;
				editButton.addEventListener(MouseEvent.CLICK, function():void {
			         var editMode:EditManager = new EditManager();
					 var selectMode:SelectionManager = new SelectionManager()
					 if (userTextFlow.interactionManager.editingMode ==  "readSelect") {
						    userTextFlow.interactionManager = editMode; 						 
						 }else {
							userTextFlow.interactionManager = selectMode; 
						 }
				} );
				addChild(editButton)
				
				
				resultButton = new Button();
				resultButton.label = "PrintResult";
				resultButton.x = 500
				resultButton.y = 90;
				resultButton.addEventListener(MouseEvent.CLICK, function():void {
					

				} );
				addChild(resultButton)
			 
		}
		private var _BorderLine:Sprite = new Sprite();

			
		public function set border(value:Boolean):void {
			var cc:ContainerController = userTextFlow.flowComposer.getControllerAt(0)   
			if (value) {
				_BorderLine.graphics.lineStyle(1, 0x000000, 1);
				_BorderLine.graphics.drawRect(0, 0, cc.compositionWidth, cc.compositionHeight);
				this.addChild(_BorderLine);
				}else {
				this.removeChild(_BorderLine);
				}
			}
		
		public function get border():Boolean {
				return this.contains(_BorderLine);
			}
		
		public function  addFace(source:Object, width:Object, height:Object):void {
			
			  EditManager(userTextFlow.interactionManager).insertInlineGraphic(source, width, height);
			  userTextFlow.interactionManager.setFocus();
			}
			
		 
		public function movetoTextEnd():void {
			userTextFlow.interactionManager.selectRange(userTextFlow.getText().length,userTextFlow.getText().length);
			userTextFlow.interactionManager.setFocus();
			}
			
		public function addEventText(text:String, parameter:String,callback:Function):void {
			  _triggerText(text, parameter, 0xFF00CC).addEventListener(MouseEvent.CLICK,callback);
			}	

		public function  _triggerText(text:String, parameter:String,color:Object):LinkElement {
			    var SWhite:SpanElement = new SpanElement();
				    SWhite.text = " ";
                    ParagraphElement(userTextFlow.getChildAt(0)).addChild(SWhite); 
					 
				var linkspan:SpanElement = new SpanElement();
					linkspan.text = "["+text+"]";
					linkspan.color = color;
					linkspan.textDecoration = "none";
					linkspan.paddingRight=5
					 
		        var  link:LinkElement = new LinkElement();
			         link.id = parameter;
					 link.addChild(linkspan);
					 link.paragraphSpaceAfter = 200;
					 ParagraphElement(userTextFlow.getChildAt(0)).addChild(link);
					
				var EWhite:SpanElement = new SpanElement();
					EWhite.text = " ";
					ParagraphElement(userTextFlow.getChildAt(0)).addChild(EWhite); 
				    
					userTextFlow.flowComposer.updateAllControllers();
				    movetoTextEnd();
					return link;
			}	
		
			public function addProps(text:String,parameter:String):void {
			  
			}
		 
			
		public function showMenu():void {			    	
						if (this.contains(_p)) {
							removeChild(_p)
							}
						_p.x = _stage.mouseX
						_p.y = _stage.mouseY
						addChild(_p);
						
						setTimeout(function():void { 
							if (contains(_p)) 
							removeChild(_p)
							}, 2000);
				}	
				
		public function parse(tf:TextFlow):String {			
			   var p:ParagraphElement = userTextFlow.getChildAt(0) as ParagraphElement;
					 for (var i:int = 0; i < p.numChildren; i++) 
					 {
						 if (p.getChildAt(i) is SpanElement) {
							 trace("/T:"+SpanElement(p.getChildAt(i)).getText())
							 }
							 
						if (p.getChildAt(i) is InlineGraphicElement) {
							trace("/F:"+InlineGraphicElement(p.getChildAt(i)).source) 
							//trace( SpanElement(p.getChildAt(i)).getText())
							 }
						if (p.getChildAt(i) is LinkElement) {
						     trace("/E:"+LinkElement(p.getChildAt(i)).getText()　+ LinkElement(p.getChildAt(i)).id);
							}
						 //trace(p.getChildAt(i))
					 }
					 return null
			}
			
		static public function addNewChat(message:String):void {
				
				
				}
	}

}